package _07_串;

import java.util.*;

public class _500_键盘行 {

    public static void main(String[] args) {

        _500_键盘行 v = new _500_键盘行();

       /* String[] str = {
                "Hello","Alaska","Dad","Peace"
        };*/
        String[] str = {
                "omk"
        };
        System.out.println(Arrays.toString(v.findWords(str)));

    }

    private static final char[][] data = {
            {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'},
            {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'},
            {'z', 'x', 'c', 'v', 'b', 'n', 'm'},
    };

    public String[] findWords(String[] words) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                map.put((int) data[i][j], i);
            }
        }

        List<String> results = new ArrayList<>();

        for (String word : words) {
            int len = word.length();
            int flag = -1;
            for (int i = 0; i < len; i++) {
                char c = word.charAt(i);
                int row = map.get(c < 'a'? c + 32: c);
                if (flag != -1 && flag != row) {
                    flag = -1;
                    break;
                }
                flag = row;
            }
            if (flag != - 1) {
                results.add(word);
            }
        }
        return results.toArray(new String[0]);
    }

}
